/**
 * 自定义函数
 * */


//列表
function getIndex(){
    var uinfo = getAdminInfo();
    if (!uinfo) { return }
    var formData = {
        role_id: uinfo.role_id,
        depart_id: uinfo.depart_id,
        uid: uinfo.uid,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/index',
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code < 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            tbody.html(getHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//列表字符串
function getHtml(d){
    var h = '', i;
    h += '';
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].item_name + '</td>';
        h += '<td>' + d[i].task_name + '</td>';
        h += '<td>' + d[i].wh_str + '</td>';
        h += '<td>' + d[i].delay_str + '</td>';
        h += '<td>' + d[i].staff_name + '</td>';
        h += '<td>' + d[i].lasttime + '</td>';
        h += '</td>';
        h += '<td class="td-manage">';
        h += '<a href="javascript:;" class="ml-5" onclick="window.location.href=\'wh-edit.html?id=' + d[i].id + '\';">';
        h += '编辑';
        h += '<i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a>';
        h += '&nbsp;&nbsp;';
        h += '<a href="javascript:;" class="ml-5" onclick="lay_del(' + d[i].id + ');" style="color: red;">';
        h += ' 删除';
        h += '<i class="Hui-iconfont"></i>';
        h += '</a>';
        h += '</td>';
        h += '</tr>';
    }
    return h;
}


//设置项目
function setByItem(){
    var uinfo = getAdminInfo();
    var item_id = $('#item_id').val();
    var task_id = $('#task_id').val();
    var formData = {
        id: id, item_id: item_id, task_id: task_id, uid: uinfo.uid,
        role_id: uinfo.role_id, depart_id: uinfo.depart_id,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/setbyitem',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            //window.location.reload();
            window.location.href = 'wh-edit.html?id=' + res.data;
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//选项
function getOne(){
    var uinfo = getAdminInfo();
    var formData = {
        id: id, depart_id: uinfo.depart_id,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/one',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            console.log(res.data);
            oneHtml(res.data);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//详情HTML
function oneHtml(e){
    var d = e.data;
    var itemList = e.item;
    var taskList = e.task;
    var h, i;
    //日期
    $('#in_day')[0].value = d.in_day;
    //项目
    h = '';
    if (d.item_id == 0) {
        h += '<option value="0" selected>-选择项目-</option>';
    } else {
        h += '<option value="0">-选择项目-</option>';
    }
    for (i = 0; i < itemList.length; i ++) {
        if (d.item_id == itemList[i].id) {
            h += '<option value="' + itemList[i].id + '" selected>';
        } else {
            h += '<option value="' + itemList[i].id + '">';
        }
        h += itemList[i].title;
        h += '</option>';
    }
    $('#item_id').html(h);
    //任务
    h = '';
    if (d.task_id == 0) {
        h += '<option value="0" selected>-选择任务-</option>';
    } else {
        h += '<option value="0">-选择任务-</option>';
    }
    for (i = 0; i < taskList.length; i ++) {
        if (d.task_id == taskList[i].id) {
            h += '<option value="' + taskList[i].id + '" selected>';
        } else {
            h += '<option value="' + taskList[i].id + '">';
        }
        h += taskList[i].title;
        h += '</option>';
    }
    $('#task_id').html(h);
    //工时项
    var dwh = d.whList, ii, child;
    h = '';
    for (i = 0; i < dwh.length; i ++) {
        child = dwh[i].child;
        if (child.length > 0) {
            h += '<style type="text/css">.formGroup { margin: 10px 3vw; border: 1px solid #eee; clear: both; } .formBox { margin: 20px; height: 200px; border: 1px solid #eee; clear: both; } .gsSub { padding: 0 10px; padding-top: 5px; color: #0088CC; float: right; } .wh-child { width: 85%; }</style>';
            h += '<div class="formGroup">';
            h += '<div class="row cl">';
            h += '<label class="form-label col-xs-4 col-sm-2">';
            h += '父级工时：';
            h += '</label>';
            h += '<div class="formControls col-xs-8 col-sm-9">';
            h += '<input type="text" class="input-text wh-child" value="' + dwh[i].config_title + '" disabled/>';
            h += '<a href="javascript:;" class="gsSub" onclick="child_add(' + dwh[i].id + ');">新增子级</a>';
            h += '<input type="hidden" id="config_whid' + dwh[i].id + '" value="' + dwh[i].config_whid + '"/>';
            h += '<input type="hidden" id="config_title' + dwh[i].id + '" value="' + dwh[i].config_title + '"/>';
            h += '</div>';
            h += '</div>';
        }
        for (ii = 0; ii < child.length; ii ++) {
            h += '<div class="formBox">';
            h += '<div class="row cl">';
            h += '<label class="form-label col-xs-4 col-sm-2">';
            h += '子级工时：';
            h += '</label>';
            h += '<div class="formControls col-xs-8 col-sm-9">';
            h += '<input type="text" class="input-text wh-child" value="' + child[ii].title + '" disabled/>';
            h += '<a href="javascript:;" class="gsSub" onclick="child_edit(' + child[ii].id + ');">再次编辑</a>';
            h += '<input type="hidden" id="pid' + child[ii].id + '" value="' + dwh[i].id + '"/>';
            h += '</div>';
            h += '</div>';
            h += '<div class="row cl">';
            h += '<label class="form-label col-xs-4 col-sm-2">';
            h += '开始时间段：';
            h += '</label>';
            h += '<div class="formControls col-xs-8 col-sm-9">';
            h += '<input type="text" class="input-text wh-child" value="' + child[ii].starthour + '" disabled/>';
            h += '<a href="javascript:;" class="gsSub" style="color: red;" onclick="itemDel(' + child[ii].id + ');">删除</a>';
            h += '</div>';
            h += '</div>';
            h += '<div class="row cl">';
            h += '<label class="form-label col-xs-4 col-sm-2">';
            h += '结束时间段：';
            h += '</label>';
            h += '<div class="formControls col-xs-8 col-sm-9">';
            h += '<input type="text" class="input-text wh-child" value="' + child[ii].endhour + '" disabled/>';
            h += '</div>';
            h += '</div>';
            h += '<div class="row cl">';
            h += '<label class="form-label col-xs-4 col-sm-2">';
            h += '时长：';
            h += '</label>';
            h += '<div class="formControls col-xs-8 col-sm-9">';
            h += '<input type="text" class="input-text wh-child" value="' + child[ii].delay_str + '" disabled/>';
            h += '</div>';
            h += '</div>';
            h += '</div>';
        }
        h += '</div>';
    }
    $('#formEdit').html(h);
}


//新增父级
function one_add(title,url){
    var item_id = $('#item_id').val();
    if (item_id == 0) {
        layerMsgErr('项目必须的'); return
    }
    url += '?id=' + 0;
    url += '&wh_id=' + id;
    layer_show(title,url,null,380);
}


//新增子级
function child_add(v){
    var config_whid = $('#config_whid' + v).val();
    var config_title = $('#config_title' + v).val();
    //console.log(v, config_whid, config_title); return;
    var item_id = $('#item_id').val();
    if (item_id == 0) {
        layerMsgErr('项目必须的'); return
    }
    var title = '修改子工时';
    var url = 'wh-item.html';
    url += '?id=' + 0;
    url += '&wh_id=' + id;
    url += '&config_whid=' + config_whid;
    url += '&config_title=' + escape(config_title);
    layer_show(title,url,null,380);
}


//修改子级
function child_edit(v){
    var pid = $('#pid' + v).val();
    var config_whid = $('#config_whid' + pid).val();
    var config_title = $('#config_title' + pid).val();
    var item_id = $('#item_id').val();
    if (item_id == 0) {
        layerMsgErr('项目必须的'); return
    }
    var title = '修改子工时';
    var url = 'wh-item.html';
    url += '?id=' + v;
    url += '&wh_id=' + id;
    url += '&config_whid=' + config_whid;
    url += '&config_title=' + escape(config_title);
    layer_show(title,url,null,380);
}


//删除
function itemDel(v){
    var formData = {
        id: v,
    };
    //console.log(formData);
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'whitem/del',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '成功'});
            setTimeout(function(){
                window.top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//删除
function lay_del(id) {
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg, function (index) {
        $.ajax({
            type: apiMethod,
            url: apiDomain + 'wh/del',
            data: {  id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg});
                    return
                }
                layer.msg('已删除!', {icon: 1, time: 1000});
                setTimeout(function () {
                    top.location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


//列表
function authIndex(){
    var uinfo = getAdminInfo();
    if (!uinfo) { return }
    var formData = {
        role_id: uinfo.role_id,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/auth-index',
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code < 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            tbody.html(authHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//列表字符串
function authHtml(d){
    var h = '', i, ii, whList;
    h += '';
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].item_name + '</td>';
        h += '<td>' + d[i].task_name + '</td>';
        h += '<td>';
        whList = d[i].whList;
        for (ii = 0; ii < whList.length; ii ++) {
            h += whList[ii].title;
            h += '：' + whList[ii].starthour;
            h += '~' + whList[ii].endhour;
            h += '，时长 ' + whList[ii].delay + '分钟 <br>';
        }
        h += '</td>';
        h += '<td>' + d[i].staff_name + '</td>';
        h += '<td>' + d[i].lasttime + '</td>';
        h += '</td>';
        h += '<td class="td-manage">';
        h += '<a href="javascript:;" class="ml-5" onclick="lay_auth_ok(' + d[i].id + ');" style="color: green;">';
        h += '<b>通过</b>';
        h += '</a>';
        h += '<a href="javascript:;" class="ml-5" onclick="lay_auth_no(\'审核拒绝\',\'wh-auth-no.html\', ' + d[i].id + ');" style="color: red;">';
        h += '<b>拒绝</b>';
        h += '</a>';
        h += '</td>';
        h += '</tr>';
    }
    return h;
}


//审核通过
function lay_auth_ok(id) {
    var msg = '确认要 <b style="color: red; font-size: 20px;">通过</b> 吗？';
    layer.confirm(msg, function (index) {
        $.ajax({
            type: apiMethod,
            url: apiDomain + 'wh/auth-ok',
            data: {  id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg});
                    return
                }
                layer.msg('已通过!', {icon: 1, time: 1000});
                setTimeout(function () {
                    top.location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


//审核拒绝弹窗
function lay_auth_no(title,url,id,w,h){
    url += '?id=' + id;
    //var tt = $('#item_id' + id).val();
    //tt = tt ? escape(tt) : '';
    //url += '&item_id=' + tt;
    h = 350;
    layer_show(title,url,w,h);
}


//审核拒绝
function auth_no() {
    var remark = $('#remark').val();
    if (!remark) {
        msgPop({msg: '缘由必须的'}); return
    }
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/auth-no',
        data: {  id: id, remark: remark },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            layer.msg('已拒绝!', {icon: 1, time: 1000});
            setTimeout(function () {
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}